<?php
class Default_Model_TableColumn_GiaoDich {
	const DATE_FORMAT_FULL_EN = 'Y-m-d H:i:s';
	const DATE_FORMAT_FULL_VN = 'd/m/Y H:i:s';
	const DATE_FORMAT_SHORT_VN = 'd-m-Y';
	protected $id;
	protected $url_tin;
	protected $goi_tin;
	protected $nguoi_dung;
	protected $ngay_thuc_hien;


	public function __construct(array $options = null) {
		if (is_array($options)) {
			$this->setOptions($options);
		}
	}
	
	public function __set($name, $value) {
		$method = 'set' . $name;
		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid content property');
		}
		$this->$method($value);
	}
	
	public function __get($name) {
		$method = 'get' . $name;
		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid content property');
		}
		return $this->$method();
	}
	
	public function setOptions(array $options) {
		$methods = get_class_methods($this);
		foreach ($options as $key => $value) {
			$method = 'set' . ucfirst($key);
			if (in_array($method, $methods)) {
				$this->$method($value);
			}
		}
		return $this;
	}
	
	//id
	public function getId() {
		return $this->id;
	}
	
	public function setId($id) {
		$this->id = $id;
	}
	
	//url_tin
	public function getUrl_tin() {
		return $this->url_tin;
	}
	
	public function setUrl_tin($url_tin) {
		$this->url_tin = $url_tin;
	}
	
	//goi_tin
	public function getGoi_tin() {
		return $this->goi_tin;
	}
	
	public function setGoi_tin($goi_tin) {
		$this->goi_tin = $goi_tin;
	}
	
	//nguoi_dung
	public function getNguoi_dung() {
		return $this->nguoi_dung;
	}
	
	public function setNguoi_dung($nguoi_dung) {
		$this->nguoi_dung = $nguoi_dung;
	}
	
	//ngay_thu_hien
	public function getNgay_thuc_hien($format = self::DATE_FORMAT_FULL_VN) {
		return date($format,strtotime($this->ngay_thuc_hien));
	}
	
	public function setNgay_thuc_hien($value) {
		$this->ngay_thuc_hien = date(self::DATE_FORMAT_FULL_EN, strtotime($value));
	}
	
	//Get reference
	//goi tin
	public function getGoiTinObj() {
		$goitin = new Default_Model_GoiTinMapper();
		if($this->getGoi_tin() != null){
			foreach ($goitin->fetchAll('id = ' . $this->getGoi_tin()) as $goi_tin_obj)
			return $goi_tin_obj;
		}
	}

	//nguoi dung
	public function getNguoiDungObj() {
		$user = new Default_Model_UsersMapper();
		if($this->getNguoi_dung() != null){
        	$user_obj = $user->fetchAll('id='. $this->getNguoi_dung(), 'view');
			return $user_obj;
		}
	}

	//to array
	public function toArray() {
		$data = array(
			'id'			=> $this->id,
			'url_tin'		=> $this->url_tin,
			'goi_tin'		=> $this->goi_tin,
			'nguoi_dung'	=> $this->nguoi_dung,
			'ngay_thuc_hien'=> $this->ngay_thuc_hien,
		);
		$item = array();
		foreach ($data as $key => $value) {
			if ($value != ''&& $value != null) {
				$item[$key] = $value;
			}
		}
		return $item;
	}
}